package com.acquisition.config.security.handler;

import com.acquisition.common.entity.BaseResponse;
import com.acquisition.common.enums.ResultCode;
import com.alibaba.fastjson.JSON;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author Jay Chou
 * @Description 匿名用户访问无权限资源时的异常
 * @Date 2023-04-12 10:57
 **/
@Component
public class CustomizeAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException {
        httpServletResponse.setContentType("text/json;charset=utf-8");
        httpServletResponse.getWriter().write(JSON.toJSONString(new BaseResponse(ResultCode.USER_NOT_LOGIN)));
    }
}
